
/*---------------------------------------------------------------------------------------------------------*/
/* Nuvoton Corporation confidential                                                            */
/*                                                                                                         */
/* Copyright (c) 2009 by Nuvoton Corporation                                                   */
/* All rights reserved                                                                                     */
/*                                                                                                         */
/*---------------------------------------------------------------------------------------------------------*/
/* File Contents:                                                                                          */
/*   rtc_diagnostic.c_                                                                                     */
/*            This file contains                                                                           */
/* Project:   DA8205                                                                                       */
/*---------------------------------------------------------------------------------------------------------*/

/*---------------------------------------------------------------------------------------------------------*/
/* Includes of system headers                                                                              */
/*---------------------------------------------------------------------------------------------------------*/
#include <stdio.h>
#include <string.h>
#include "NUC900_rtc.h"

/*---------------------------------------------------------------------------------------------------------*/
/* Includes of local headers                                                                               */
/*---------------------------------------------------------------------------------------------------------*/

/*---------------------------------------------------------------------------------------------------------*/
/* Macro, type and constant definitions                                                                    */
/*---------------------------------------------------------------------------------------------------------*/
#define printf sysprintf

/*---------------------------------------------------------------------------------------------------------*/
/* Global variables                                                                                        */
/*---------------------------------------------------------------------------------------------------------*/
INT i=0;
INT RTC_FD;
INT nRet;
CHAR Week[7][10] = {"Sunday   ",
                    "Monday   ",
                    "Tuesday  ",
                    "Wednesday",
                    "Thursday ",
                    "Friday   ",
                    "Saturday " };

RTC_TIME_DATA_T td;
RTC_TICK_T tkd;

BOOL volatile IsAlarmCallBack=0;
BOOL volatile IsTickCallBack=0;
BOOL volatile IsWakeUpCallBack=0;

/*---------------------------------------------------------------------------------------------------------*/
/* Function: <DisplayRTCTime>                                                                              */
/*                                                                                                         */
/* Parameter:                                                                                              */
/*              VOID                                                                                       */
/* Returns:                                                                                                */
/*              VOID                                                                                       */
/* Description:                                                                                            */
/*              Show right now time                                                                        */
/*---------------------------------------------------------------------------------------------------------*/

VOID DisplayRTCTime(VOID)
{
	INT nRet;

    memset(&td, 0, sizeof(RTC_TIME_DATA_T));
    
    nRet = RTC_Read(CURRENT_TIME,&td);
    
    if (nRet == 0)
    {
        if (td.ucClockDisplay == RTC_CLOCK_12)                            /* 12 hour format                */
        {
            printf("%d.%02d.%d %s %02d:%02d:%02d %s\n", td.uYear, td.ucMonth, td.ucDay,
                       &Week[td.ucDayOfWeek][0], td.ucHour, td.ucMinute, td.ucSecond,
                       (td.ucAmPm == RTC_AM) ? "AM" : "PM");
        }
        else                                                             /* 24 hour format                 */
        {
            printf("%d.%02d.%d %s %02d:%02d:%02d\n", td.uYear, td.ucMonth, td.ucDay,
                       &Week[td.ucDayOfWeek][0], td.ucHour, td.ucMinute, td.ucSecond);
        }
    }
    else
    {
        printf("\n Get rtc time data fail [%x]\n", nRet);
    }    
}

/*---------------------------------------------------------------------------------------------------------*/
/* Function: <DisplayAlarmTime>                                                                            */
/*                                                                                                         */
/* Parameter:                                                                                              */
/*              VOID                                                                                       */
/* Returns:                                                                                                */
/*              VOID                                                                                       */
/* Description:                                                                                            */
/*              Read RTC Alarm Time Setting                                                                */
/*---------------------------------------------------------------------------------------------------------*/

VOID DisplayAlarmTime(VOID)
{
    INT nRet;

    memset(&td, 0, sizeof(RTC_TIME_DATA_T));
    nRet = RTC_Read( ALARM_TIME, &td);
    
    
    if (nRet == 0)
    {
        if ( (td.ucAmPm == RTC_AM) || (td.ucAmPm == RTC_PM) )
        {
            printf("%d.%02d.%d %02d:%02d:%02d %s\n", td.uYear, td.ucMonth, td.ucDay,
                       td.ucHour, td.ucMinute, td.ucSecond, (td.ucAmPm == RTC_AM) ? "AM" : "PM");
        }
        else                                                            /* 24 hour format                  */
        {
            printf("%d.%02d.%d %02d:%02d:%02d\n", td.uYear, td.ucMonth, td.ucDay,
                       td.ucHour, td.ucMinute, td.ucSecond);
        }
    }
    else
    {
        printf("\n Get rtc time data fail [%x]\n", nRet);
    }    
}

/*---------------------------------------------------------------------------------------------------------*/
/* Function: <AlarmCallBackDemo>                                                                           */
/*                                                                                                         */
/* Parameter:                                                                                              */
/*              VOID                                                                                       */
/* Returns:                                                                                                */
/*              VOID                                                                                       */
/* Description:                                                                                            */
/*                                                                                                         */
/*---------------------------------------------------------------------------------------------------------*/
VOID AlarmCallBackDemo(VOID)
{
    
    printf(" Alarm OK\n");
    IsAlarmCallBack=1;
 
}

/*---------------------------------------------------------------------------------------------------------*/
/* Function: <TickCallBackDemo>                                                                            */
/*                                                                                                         */
/* Parameter:                                                                                              */
/*              VOID                                                                                       */
/* Returns:                                                                                                */
/*              VOID                                                                                       */
/* Description:                                                                                            */
/*                                                                                                         */
/*---------------------------------------------------------------------------------------------------------*/

VOID TickCallBackDemo(VOID)
{
 	printf("Tick INT OK \n");   
    IsTickCallBack=1;
   
}

VOID WakeupCallBackDemo()
{
	
	for(i=0;i<0xA0000;i++);
	printf("Wake up OK \n");
	IsWakeUpCallBack=1;

}

/*---------------------------------------------------------------------------------------------------------*/
/* Function: <TestItem>                                                                                    */
/*                                                                                                         */
/* Parameter:                                                                                              */
/*              VOID                                                                                       */
/* Returns:                                                                                                */
/*              VOID                                                                                       */
/* Description:                                                                                            */
/*              Set up Test Intial Menu                                                                    */
/*---------------------------------------------------------------------------------------------------------*/

VOID TestItem(VOID)
{
    printf("\n\n");
    printf("+-----------------------------------+\n");
    printf("|   Nuvoton RTC Diagnostic Code     |\n");
    printf("+-----------------------------------+\n");
    printf("| Set RTC Current Time Data   - [0] |\n");
    printf("| Get RTC Current Time Data   - [1] |\n");
    printf("| Set RTC Alarm Time Data     - [2] |\n");
    printf("| Get RTC Alarm Time Data     - [3] |\n");
    printf("| Identify Leap Year          - [4] |\n");
    printf("| Set RTC Tick Mode           - [5] |\n");
    printf("| Disable RTC Tick Interrupt  - [6] |\n");
    printf("| Get RTC Tick                - [7] |\n");
    printf("| Restore RTC Tick            - [8] |\n");
    printf("| Wakeup RTC                  - [9] |\n");
    printf("+-----------------------------------+\n");
    printf("| Exit Test Program           - [q] |\n");
    printf("+-----------------------------------+\n");
}

VOID RTC_SetTime()
{
	   

      td.ucClockDisplay = RTC_CLOCK_12;  /* Set 12-hour mode have to set ucAmPm                  */
      td.ucAmPm = RTC_AM;

      td.uYear = 2007;
      td.ucMonth = 1;
      td.ucDay = 3;
      td.ucDayOfWeek = WEDNESDAY;
      td.ucHour = 11;
      td.ucMinute = 59;
      td.ucSecond = 55;
      nRet = RTC_Write(CURRENT_TIME, &td);
      
      printf("Set RTC Current time data ...");
      if (nRet == 0)
      {
          
          printf("OK \n");
      }    
      else
      {
          printf("Fail\n");
	  }
      
      td.ucClockDisplay = RTC_CLOCK_12;  /* Set 12-hour mode have to set ucAmPm                  */
      td.ucAmPm = RTC_AM;

      td.uYear = 2008;
      td.ucMonth = 2;
      td.ucDay = 22;
      td.ucDayOfWeek = WEDNESDAY;
      td.ucHour = 03;
      td.ucMinute = 02;
      td.ucSecond = 01;
      nRet = RTC_Write(ALARM_TIME, &td);
      printf("Set RTC Alarm time data .....");
      if (nRet == 0)
      {
      	  
          printf("OK \n");
      }    
      else
      {
          printf("Fail\n");
	  }
	  

}


VOID RTC_AlarmTest()
{
    printf("RTC Alarm Test........");
    IsAlarmCallBack = 0;
    
    td.ucClockDisplay = RTC_CLOCK_12;   /* Be sure to set RTC first.                   */
    td.ucAmPm         = RTC_AM;
    td.uYear          = 2009;
    td.ucMonth        = 5;
    td.ucDay          = 6;
    
    td.ucDayOfWeek    = WEDNESDAY;
    td.ucHour         = 5;
    td.ucMinute       = 0;
    td.ucSecond       = 0;
    
    RTC_Write(CURRENT_TIME,&td);
    
    
    td.ucAmPm = RTC_AM;  /* if set RTC as 12-hr mode, should set ucAmPm for alarm      */
    td.uYear          = 2009;
    td.ucMonth        = 5;
    td.ucDay          = 6;
    
    td.ucHour         = 5;
    td.ucMinute       = 0;
    td.ucSecond       = 1;
    td.fnAlarmCallBack = AlarmCallBackDemo;
    
    RTC_Write(ALARM_TIME, &td);
    
    //printf(" Wait 5 sec...");
    
    
    while(IsAlarmCallBack==0);
       
}
VOID RTC_LeapTest()
{
	INT val,i;
	td.ucClockDisplay = RTC_CLOCK_24;                       /* Be sure to set RTC first.       */
    td.uYear = 2004;
    td.ucMonth = 2;
    td.ucDay = 28;
    td.ucDayOfWeek = MONDAY;
    td.ucHour = 23;
    td.ucMinute = 59;
    td.ucSecond = 55;
    
    printf("RTC leap year test...");
    RTC_Write(CURRENT_TIME,&td);
    
    for(i=0;i<5000;i++);
    RTC_Ioctl(RTC_FD, RTC_IOC_IDENTIFY_LEAP_YEAR, (UINT32)&val, 0);/* Check leap year.  */
   
    if(val == RTC_LEAP_YEAR)
    {
    	printf("........OK \n");
    }
    else
    {
        printf("FAILED \n");
    }    
              

}

VOID RTC_TickINTTest()
{
	UINT32 btime;
	tkd.ucMode = RTC_TICK_1_SEC;    
    tkd.fnTickCallBack = TickCallBackDemo;
    printf("RTC Tick INT Test......");
    IsTickCallBack = 0;
    RTC_Ioctl(RTC_FD, RTC_IOC_SET_TICK_MODE, (UINT32)&tkd, 0);
    
  
    RTC_Ioctl(RTC_FD,RTC_IOC_ENABLE_INT, TICK_INT, 0);/* Enable tick interrupt  */
     
    sysStartTimer(TIMER0, 1000, PERIODIC_MODE);
    
    btime = sysGetTicks(TIMER0);
	
    //while(IsTickCallBack==0);
    
    while(IsTickCallBack==0)
    {
        btime = sysGetTicks(TIMER0);
        
        if((sysGetTicks(TIMER0)-btime)>=1000)
        	sysprintf(".");
    
    } 
   
      //for(ii=0;ii<0x600000;ii++); 
	// printf(" Tick OK \n");
}

VOID RTC_WakeupTest()
{
    //INT i;
    
    printf("RTC Wake up test.......");
    IsWakeUpCallBack = 0;
    td.ucClockDisplay = RTC_CLOCK_24;            /* Set Loading & Alarm Time Data              */
    td.uYear          = 2007;
    td.ucMonth        = 05;
    td.ucDay          = 16;
    td.ucDayOfWeek    = WEDNESDAY;
    td.ucHour         = 9;
    td.ucMinute       = 3;
    td.ucSecond       = 0;
    RTC_Write(CURRENT_TIME, &td);

    td.uYear          = 2007;
    td.ucMonth        = 05;
    td.ucDay          = 16;
    td.ucHour         = 9;
    td.ucMinute       = 3;
    td.ucSecond       = 1;
    td.fnAlarmCallBack = WakeupCallBackDemo;
    
    RTC_Write(ALARM_TIME, &td);
     
     
     RTC_Ioctl(RTC_FD,RTC_IOC_ENABLE_INT, WAKEUP_INT, 0); 


	writew(REG_PMCON,0x1); // idle mode
	
	while(IsWakeUpCallBack==0);
	
	
	
             
}



VOID RTC_ReadTest()
{
	INT nRet,count=0,btime=0;
	
	printf("Get RTC Time Test......\t\t\n");
	
	do{    
    	memset(&td, 0, sizeof(RTC_TIME_DATA_T));
    
    	nRet = RTC_Read(CURRENT_TIME,&td);
    
    	if (nRet == 0)
    	{
    
        	if (td.ucClockDisplay == RTC_CLOCK_12)                            /* 12 hour format                */
        	{
            	printf("\t %d.%02d.%d %s %02d:%02d:%02d %s\n", td.uYear, td.ucMonth, td.ucDay,
                       &Week[td.ucDayOfWeek][0], td.ucHour, td.ucMinute, td.ucSecond,
                       (td.ucAmPm == RTC_AM) ? "AM" : "PM");
       	 	
       	 	}
        	else                                                              /* 24 hour format                 */
        	{
            	printf("\t %d.%02d.%d %s %02d:%02d:%02d\n", td.uYear, td.ucMonth, td.ucDay,
                       &Week[td.ucDayOfWeek][0], td.ucHour, td.ucMinute, td.ucSecond);
        	}
        	count++;
    	}
    	else
    	{
        	printf("\n Get rtc time data fail [%x]\n", nRet);
        	
    	}
   	//for(ii=0;ii<0x800000;ii++);
   
    #if 1 	
    	    
    	sysStartTimer(TIMER0, 1000, PERIODIC_MODE);
		btime = sysGetTicks(TIMER0);
		while(1)
		{
	    	if(sysGetTicks(TIMER0) - btime >=1000)
	    	{
	    		break;
	    	}
	    
	    }
	    //for(ii=0;ii<0x600000;ii++); 
	#endif	
    	
	}while(count<6);
	
	printf(".............................OK \n");

}



/*---------------------------------------------------------------------------------------------------------*/
/* Function:    <Main>                                                                                     */
/*                                                                                                         */
/* Parameter:                                                                                              */
/*              VOID                                                                                       */
/* Returns:                                                                                                */
/*              0    Successful                                                                            */
/* Description:                                                                                            */
/*              Main Function                                                                              */
/*---------------------------------------------------------------------------------------------------------*/
INT diag_rtc(VOID)
{
    WB_UART_T	my_uart;    
  
   	//CHAR cUserSelect;
                                            
    /* UART setting */

	my_uart.uiFreq     = 15000000; /* 15MHZ */
	my_uart.uiBaudrate = 115200;
	my_uart.uiDataBits = WB_DATA_BITS_8;
	my_uart.uiStopBits = WB_STOP_BITS_1;
	my_uart.uiParity   = WB_PARITY_NONE;
	my_uart.uiRxTriggerLevel = LEVEL_1_BYTE;
	
	sysInitializeUART(&my_uart);
	//printf("\n >>>>>>RTC Diagnositic test<<<<<<\n");
    
    nRet = RTC_Init();
    
    if (nRet != 0)
    {
        printf("FAILED \n");
        return 0;
    }
    else
    {
   	    printf("\n\nRTC INIR.....................OK \n");
    }
#if 0 
    TestItem();
	 while(1)
	 {  
	    cUserSelect = sysGetChar(); 
	    printf("Choose [%c] \n",cUserSelect);
	 }   
    
    printf("| Set RTC Current Time Data   - [0] |\n");
    printf("| Get RTC Current Time Data   - [1] |\n");
    printf("| Set RTC Alarm Time Data     - [2] |\n");
    printf("| Get RTC Alarm Time Data     - [3] |\n");
    printf("| Identify Leap Year          - [4] |\n");
    printf("| Set RTC Tick Mode           - [5] |\n");
    printf("| Disable RTC Tick Interrupt  - [6] |\n");
    printf("| Get RTC Tick                - [7] |\n");
    printf("| Restore RTC Tick            - [8] |\n");
    printf("| Wakeup RTC                  - [9] |\n");
#endif    
  
    RTC_SetTime();
    RTC_ReadTest();
    RTC_LeapTest();
    RTC_AlarmTest();
    RTC_TickINTTest();
	RTC_WakeupTest();
	
	sysStopTimer(TIMER0);
	sysStartTimer(TIMER0, 100, PERIODIC_MODE);	
 	RTC_Close();  
    return 0;
}
